package com.qidian.mes.pro.service.impl;

import com.qidian.mes.pojo.Technique;
import com.qidian.mes.pro.mapper.TechniqueMapper;
import com.qidian.mes.pro.service.TechniqueService;
import com.qidian.mes.util.dot.PagerDTO;
import com.qidian.mes.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author dqy
 * @Description
 * @Time 2024/4/11 16:47
 */
@Service
public class TechniqueServiceImpl implements TechniqueService {
    @Autowired
    private TechniqueMapper techniqueMapper;
    @Override
    public QdResult saveTechnique(Technique technique) {
        List<Technique> techniqueList = techniqueMapper.selectByCode(technique.getTechniqueCode());
        if (techniqueList != null){ //有可能存在
           for (Technique t:techniqueList){//多个99 一个1
               if (t.getTechniqueStatus() != 99){
                   return QdResult.duplicate("工艺编码已存在");
               }
           }
        }//不存在
        List<Technique> techniqueList1 = techniqueMapper.selectByName(technique.getTechniqueName());
        if (techniqueList1 != null){
            for (Technique t:techniqueList1){
                if (t.getTechniqueStatus() != 99){
                    return QdResult.duplicate("工艺名称已存在");
                }
            }
        }
        return QdResult.isSuccess(techniqueMapper.insertTechnique(technique));
    }

    @Override
    public QdResult queryById(Long techniqueId) {
        return QdResult.success(techniqueMapper.selectById(techniqueId));
    }

    @Override
    public QdResult<List<Technique>> queryAll() {
        return QdResult.success(techniqueMapper.queryAll());
    }

    @Override
    public QdResult<PagerDTO<Technique>> queryByPage(Technique technique, int start, int limit) {
        PagerDTO<Technique> pagerDTO = new PagerDTO<>();
        pagerDTO.setPage(start);
        pagerDTO.setLimit(limit);
        pagerDTO.setTotal(techniqueMapper.selectCount(technique));
        pagerDTO.setData(techniqueMapper.selectByPage(technique,(start-1)*limit,limit));
        return QdResult.success(pagerDTO);
    }

    @Override
    public QdResult updateTechnique(Technique technique) {
        if (technique.getTechniqueId() == null){
            return QdResult.error("未获取到工艺ID");
        }
        Technique techniqueSelectById = techniqueMapper.selectById(technique.getTechniqueId());
        if(technique.getTechniqueName().equals(techniqueSelectById.getTechniqueName())&&technique.getTechniqueCode().equals(techniqueSelectById.getTechniqueCode())){
            return QdResult.success(techniqueMapper.updateTechnique(technique));
        }
        List<Technique> technique1 = techniqueMapper.selectByCode(technique.getTechniqueCode());
        if (technique1 != null ){
            for (Technique t:technique1){
                if (t.getTechniqueStatus() != 99){
                    return QdResult.duplicate("工艺编码已存在");
                }
            }
        }
        List<Technique> technique2 = techniqueMapper.selectByName(technique.getTechniqueName());
        if (technique2 != null){
            for (Technique t:technique2){
                if (t.getTechniqueStatus() != 99){
                    return QdResult.duplicate("工艺名称已存在");
                }
            }
        }
        return QdResult.isSuccess(techniqueMapper.updateTechnique(technique));
    }

    @Override
    public QdResult deletes(Long[] techniqueIds) {
        techniqueMapper.deleteByIds(techniqueIds);
        return QdResult.success();
    }

    @Override
    public QdResult deleteById(Long techniqueId) {
        techniqueMapper.deleteById(techniqueId);
        return QdResult.success();
    }
}
